// 华为OD机试的3道考题

// 第一题：API集群负载统计 https://fcqian.blog.csdn.net/article/details/134635782
// 第二题：机场航班调度程序 https://fcqian.blog.csdn.net/article/details/134591347?spm=1001.2014.3001.5502
// 第三题：信道分配 https://blog.csdn.net/qfc_128220/article/details/127607871

// 如下内容为机试时的草稿

/** 
* 5
/huawei/computing/no/one
/huawei/computing
/huawei
/huawei/cloud/no/one
/huawei/wireless/no/one
2 computing
* */

// let a = 'CA3385,CZ6678,DU7523,HK4456,MK0987,SC6508';

// let strPlanes = 'CA3385,CA6678,DU7523,HK4456,MK0987,SC6508';
// let arrPlanes = strPlanes.split(',');

// arrPlanes.sort((a, b) => {
//     const bA = a.substring(0, 2).toUpperCase();
//     const bB = b.substring(0, 2).toUpperCase();

//     if (bA < bB) {
//         return -1;
//     }

//     if (bA > bB) return 1;

//     const nA = parseInt(a.substring(2));
//     const nB = parseInt(b.substring(2));

//     if (nA < nB) {
//         return -1;
//     }

//     if (nA > nA) return 1;

//     return 0;
// });

// let strRes = arrPlanes.reduce((acc, cur) => acc + (',' + cur), '');
// strRes = strRes.substring(1);

// console.log(strRes);

// var a = 30;

// a.toString(2); // 二进制输出 10111
// console.log(a.toString(2));

// let levels = parseInt(await readline()); // 阶
// let levelsCount = (await readline()).split(' '); // 每一个阶包含的信道数量
// let singleCapacity = parseInt(await readline());

/**
 * 5
10 5 0 1 3 2
30
 */

let levels = 5; // 阶
let levelsCount = '11 6 0 1 3 3'; // 每一个阶包含的信道数量
let singleCapacity = 30;

let bitCap = singleCapacity.toString(2);
while (bitCap.length < levels + 1) {
    bitCap = '0' + bitCap;
}

let arrBitCap = bitCap.split('');
let arrLevelsCount = levelsCount.split(' ').reverse();

console.log(`arrBitCap is ${arrBitCap}`);
console.log(`arrLevelsCount is ${arrLevelsCount}`);

let resCount = 0;

function arrHasMore() {
    let res = arrLevelsCount.filter((ele) => ele < 0);
    return !(res.length > 0);
}

/**
 * 从总容量中取出一个完整的用户需求信道
 */
function getOne() {
    for (let i = 0; i <= levels; i++) {
        arrLevelsCount[i] = arrLevelsCount[i] - arrBitCap[i];
    }
}

function Fill() {
    if (arrHasMore()) return;

    let arrRecover = [].concat(arrLevelsCount);
    for (let i = arrLevelsCount.length - 1; i >= 0; i--) {
        if (arrLevelsCount[i] >= 0) continue;

        let allCount = Math.abs(arrLevelsCount[i]);
        let allValue = 2 ** (levels - i) * allCount;
        // 低位是否足够
        let lowValue = 0;
        for (let j = arrLevelsCount.length - 1; j > i; j--) {
            lowValue += 2 ** (levels - j) * arrLevelsCount[j];
            if (lowValue >= allValue) {
                break;
            }
        }

        if (lowValue >= allValue) {
            let remianValue = allValue;
            // 相减

            for (let j = i + 1; j < arrLevelsCount.length; j++) {
                let remain =
                    2 ** (levels - j) * arrLevelsCount[j] - remianValue;

                arrLevelsCount[j] = Math.floor(
                    Math.max(0, remain) / 2 ** (levels - j)
                );

                if (remain >= 0) {
                    arrLevelsCount[i] = 0;
                    break;
                } else {
                    remianValue = Math.abs(remain);
                }
            }

            continue;
        }

        // 高位是否足够
        let highValue = 0;
        for (let j = i - 1; j >= 0; j--) {
            highValue += 2 ** (levels - j) * arrLevelsCount[j];
            if (highValue >= allValue) {
                break;
            }
        }

        if (highValue >= allValue) {
            // 相减
            for (let j = i - 1; j >= 0; j--) {
                if (arrLevelsCount[j] <= 0) continue;

                arrLevelsCount[j] = arrLevelsCount[j] - 1;

                // 一旦启用高位后，之前所有的低位补足都不需要了，在这个地方，需要做统一变0和复原
                for (let k = j + 1; k < arrLevelsCount.length; k++) {
                    arrLevelsCount[k] = Math.max(0, arrRecover[k]);
                }
                break;
            }
        }
    }
}

let count = 0;
while (arrHasMore()) {
    getOne();

    console.log(`minus: ${JSON.stringify(arrLevelsCount)}`);
    Fill();

    count++;

    console.log(`times ${count} remain: ${JSON.stringify(arrLevelsCount)}`);
}

count = Math.max(0, count - 1);
console.log(`final count: ${count}`);
